package Test05_06;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by 14229 on 2020/2/21.
 */
public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("第五题和第六题");
        System.out.println("1.注册   2.登录");
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        switch (i){
            case 1:
                register("a13","1234Qq");
                break;
            case 2:
                Login("a3","1234Qq");
                break;
        }

    }
    public static boolean register(String name,String password) throws IOException {
        List<String> list = new ArrayList<>();

        File f = new File("user_info.txt");
        f.createNewFile();

        List<String> lines = FileUtils.readLines(f, "UTF-8");
        if (lines.size()==0){
            list.add(name);
            if (password.matches("^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{6}$")) {
                list.add("|");
                list.add(password);
                String a = "";
                for (String line : list) {
                    a+=line;
                }
                FileUtils.writeStringToFile(f,a);
                return true;
            }

            System.out.println("密码错误");;
            list.clear();
            return false;
        }else {
            for (String line : lines) {
                String[] split = line.split("\\|");
                if (split[0].equals(name)){
                    System.out.println("用户名不能重复");
                    return false;
                }
            }
            list.add(name);
            if (password.matches("^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{6}$")) {
                list.add("|");
                list.add(password);
                String a = "";
                for (String line : list) {
                    a+=line;
                }
                a="\n"+a;
                FileUtils.writeStringToFile(f,a,true);
                return true;
            }
            System.out.println("密码错误");;
            return false;
        }

    }
    public static boolean Login(String name,String password) throws IOException {
        List<String> lines = FileUtils.readLines(new File("user_info.txt"), "UTF-8");
        for (String line : lines) {
            String[] split = line.split("\\|");
            if (split[0].equals(name)&&split[1].equals(password)){
                System.out.println("登陆成功");
                return true;
            }
        }
        System.out.println("登录失败");
        return false;
    }
}
